  <template>
    <div class="login_contain">
      <div class="login_box">
        <!-- 顶部图片 -->
        <div class="after_img">
          <img src="../assets/logo.png" alt="" />
        </div>
        <!-- 登录表单区域 -->
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          class="login-form"
          :rules="loginFormRules"
        >
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              prefix-icon="el-icon-user-solid"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              prefix-icon="el-icon-s-goods"
            ></el-input>
          </el-form-item>
          <!-- 按钮 -->
          <el-form-item class="login-btns">
            <el-button type="primary" @click="loginBtn">登录</el-button>
            <el-button type="info" @click="loginResetBtn">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </template>

  <script>
  // 引入axiso
  import axios from 'axios'
  import { loginApi } from '../request/api'
  export default {
    data() {
      return {
        // 登录表单的数据绑定对象
        loginForm: {
          username: 'admin',
          password: '123456',
        },
        loginFormRules: {
          username: [
            { required: true, message: '请输入用户名称', trigger: 'blur' },
            { min: 3, message: '长度至少 3 个字符', trigger: 'blur' },
          ],
          possword: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' },
          ],
        },
      }
    },
    methods: {
      //登录按钮  
      loginBtn() {
        // 表单的预校验
        this.$refs.loginFormRef.validate(async (valid) => {
          //   valild 为false则表单验证未通过  未true则表示验证通过
          if (!valid) return

          // const { data: res } = await this.$http.post("login", this.loginForm);
          // console.log('res');
          
          loginApi(this.loginForm).then((res) => {
            console.log(res.data)
            if (res.data.meta.status != 200)
              return this.$message.error(res.data.meta.msg)

            this.$message.success(res.data.meta.msg)
            // 将token储存在sessionstorage中
            window.sessionStorage.setItem('token', res.data.data.token)
            this.$router.push('/home')
          })

          /* 
              1，将登录成功之后的token,保存到客户端的sessionStorage中
                  1.1 项目中除了登录之外的其他api接口，必须在登录之后才能访问
                  1.12 token只应在当前网站打开期间生效，所以将token保存在sessionStorage中

              2.通过编程式导航跳转到后台主页，路由地址是 /home
          */
        })
      },
      //重置按钮  重置表单resetFields
      loginResetBtn() {
        this.$refs.loginFormRef.resetFields()
      },
    },
  }
  </script>

  <style lang="scss" scoped>
  .login_contain {
    background: #2b4b6b;
    height: 100%;
    // position: relative;
    .login_box {
      width: 450px;
      height: 300px;
      border-radius: 3px;
      background: white;
      position: absolute;
      top: -999px;
      bottom: -999px;
      left: -999px;
      right: -999px;
      margin: auto;
      .after_img {
        width: 130px;
        height: 130px;
        border-radius: 50%;
        background: white;
        box-shadow: 0 0 10px #ddd;
        padding: 10px;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        img {
          border-radius: 50%;
          width: 100%;
          height: 100%;
          background: #eee;
        }
      }
    }
    //   大表单
    .login-form {
      position: absolute;
      bottom: 0px;
      width: 100%;
      padding: 0px 20px;
      box-sizing: border-box;
    }
    //   登录重置按钮
    .login-btns {
      display: flex;
      justify-content: flex-end;
    }
  }
  </style>>
